/**
*
* Search Username JQAutocomplete
* version 1.0.0
* Copyright (c) 2010 velocat, www.velocat.ru
* License http://opensource.org/licenses/gpl-license.php GNU Public License
*
* Date: 2011-01-10 03:00:00 -0300 (10 Jan 2011)
*/

(function($) 
{
	$(function() 
	{
		
		if (usersearch_mode == 'acp')
		{
			$("#username").autocomplete({		
				url: url_username_req,
				selectFirst: true,
				minChars:2,
				extraParams: {mode:'search_username'},
				showResult: function(value, data) 
					{
						return '<span>' + hilight(value, $("#username").val())  + '</span>';
					},
				onItemSelect: function(item) {
					goto_usersearch(item.value);
				}
			});			
		}
		else
		{
		
			$("#user_live_search").autocomplete({		
				url: url_username_req,
				selectFirst: true,
				minChars:2,
				extraParams: {mode:'search_username'},
				showResult: function(value, data) 
					{
						return '<span>' + hilight(value, $("#user_live_search").val())  + '</span>';
					},
				onItemSelect: function(item) {
					goto_usersearch(item.value);
				}
			});
		}
		
		$(document).ready(function() 
		{
			goto_usersearch = function(value)
			{
				if(value)
				{
					if(usersearch_mode == 'pm')
					{					
						var old_value = $("#username_list").val();
						var new_value = (old_value)? old_value+'\n'+value : value;
						$("#username_list").val(new_value);
					}
					else if (usersearch_mode == 'acp')
					{
						$("#username").val(value);
						$("form").submit();
					}
					else
					{
						var url_search = url_searchuser.split("?");
						var sid = (url_search[1])? "&"+url_search[1] : '';	
						window.location.href = url_search[0]+"?username="+value+sid;
					}
				}
				return false;	
			}
			
		});

		hilight = function(value, term) 
		{
			return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
		}
	
	});
})(jQuery);	